package com.leostadyn.stackfarm.timestatistics.view.activity.admin.staff;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.kaopiz.kprogresshud.KProgressHUD;
import com.leostadyn.stackfarm.timestatistics.R;
import com.leostadyn.stackfarm.timestatistics.base.BaseActivity;
import com.leostadyn.stackfarm.timestatistics.model.bean.UpdateInfoBean;
import com.leostadyn.stackfarm.timestatistics.net.api.AdminApi;
import com.leostadyn.stackfarm.timestatistics.net.base.OriginalSubscriber;
import com.leostadyn.stackfarm.timestatistics.net.base.ResponseThrowable;
import com.leostadyn.stackfarm.timestatistics.utils.ToastUtil;

import java.util.HashMap;


public class StaffChangePwdActivity extends BaseActivity implements View.OnClickListener {
    TextView staff_change_et_username;
    EditText staff_change_et_password;
    EditText staff_change_et_confirm_password;
    TextView title;
    private Button staff_change_save_btn;
    private TextWatcher mTextWatcher = new TextWatcher() {
        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {

        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {

        }

        @Override
        public void afterTextChanged(Editable s) {

            checkButton();
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_staff_change_pwd);
        initView();
    }

    public void initView() {
        String name = getIntent().getStringExtra("uid");
        staff_change_et_password = findViewById(R.id.staff_change_pwd_pwd1);
        staff_change_et_username = findViewById(R.id.staff_change_pwd_ed_username);
        staff_change_et_confirm_password = findViewById(R.id.staff_change_pwd_confirm);
        staff_change_et_username.setText(name);
        title = findViewById(R.id.nav_txt_title);
        title.setText("修改密码");
        staff_change_save_btn = findViewById(R.id.staff_change_save_btn);
        staff_change_et_username.addTextChangedListener(mTextWatcher);
        staff_change_et_password.addTextChangedListener(mTextWatcher);
        staff_change_et_confirm_password.addTextChangedListener(mTextWatcher);
        staff_change_save_btn.setOnClickListener(this);
        findViewById(R.id.nav_img_back).setOnClickListener(v -> {
            finish();
        });

        String username = getIntent().getStringExtra("username");
        staff_change_et_username.setText(username);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.staff_change_save_btn:
                String staff_change_password = staff_change_et_password.getText().toString().trim();
                String satff_change_confirm = staff_change_et_confirm_password.getText().toString().trim();
                if (!checkEditTextLength()) {
                    ToastUtil.show(StaffChangePwdActivity.this, "密码长度不小于6位且不大于16位！");
                } else {
                    if (!staff_change_password.equals(satff_change_confirm)) {
                        ToastUtil.show(StaffChangePwdActivity.this, "两次密码不一致");
                        return;
                    }
                    modifyPassword();
                }
                return;

        }
    }

    public boolean checkEditTextLength() {
        String password = staff_change_et_password.getText().toString().trim();
        return password.length() <= 16 && password.length() >= 6;
    }
    private void checkButton() {
        if (isFill()) {
            staff_change_save_btn.setEnabled(true);
        } else {
            staff_change_save_btn.setEnabled(false);
        }
    }

    public boolean isFill() {
        String staff_change_id = staff_change_et_username.getText().toString().trim();
        if (TextUtils.isEmpty(staff_change_id)) {
            return false;
        }

        String staff_change_password = staff_change_et_password.getText().toString().trim();
        if (TextUtils.isEmpty(staff_change_password)) {
            return false;
        }

        String satff_change_confirm = staff_change_et_confirm_password.getText().toString().trim();
        return !TextUtils.isEmpty(satff_change_confirm);
    }

    @Override
    protected void onResume() {
        super.onResume();
        checkButton();
    }

    public void modifyPassword() {
        HashMap<String, String> map = new HashMap<>();
        String uid = getIntent().getStringExtra("uid");
        String password = staff_change_et_confirm_password.getText().toString().trim();
        map.put("uid", uid);
        map.put("password", password);

        final KProgressHUD dialog = KProgressHUD.create(this)
                .setStyle(KProgressHUD.Style.SPIN_INDETERMINATE)
                .setCancellable(true).setAnimationSpeed(2).setDimAmount(0.5f);
        dialog.show();

        AdminApi.getInstance().setPassword(map).subscribe(new OriginalSubscriber<UpdateInfoBean>(compositeDisposable) {
            @Override
            public void onError(ResponseThrowable e) {
                dialog.dismiss();
                ToastUtil.show(StaffChangePwdActivity.this, e.getMsg());
            }

            @Override
            public void onNext(UpdateInfoBean updateInfoBean, String msg) {
                dialog.dismiss();
                ToastUtil.show(StaffChangePwdActivity.this, "修改成功");
                Intent intent = new Intent();
                setResult(Activity.RESULT_OK, intent);
                finish();
            }

        });

    }
}
